package com.cloud.open.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.common.bean.PageInfo;
import com.cloud.open.dao.ResourceTypeDao;
import com.cloud.open.dto.ResourceTypeQueryParamDTO;
import com.cloud.open.entity.ResourceType;
import com.cloud.open.service.ResourceService;
import com.cloud.open.service.ResourceTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  资源分类服务实现类
 * </p>
 *
 * @author wenjin
 * @since 2019-09-25
 */
@Service
public class ResourceTypeServiceImpl extends ServiceImpl<ResourceTypeDao, ResourceType> implements ResourceTypeService {

    @Autowired
    private ResourceService resourceService;

    @Override
    public boolean checkNameExist(String name, Integer ignoreId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq(ResourceType.NAME, name);
        if (ignoreId != null) {
            wrapper.ne(ResourceType.ID, ignoreId);
        }
        List<ResourceType> resourceTypeList = list(wrapper);
        if (CollectionUtils.isEmpty(resourceTypeList)) {
            return false;
        }
        return true;
    }

    @Override
    public PageInfo<ResourceType> getPage(ResourceTypeQueryParamDTO paramDTO) {
        QueryWrapper wrapper = new QueryWrapper();
        IPage page = page(new Page(paramDTO.getPageNo(), paramDTO.getPageSize()));
        return new PageInfo(page);
    }

    @Override
    public List<ResourceType> getAll() {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.orderByAsc(ResourceType.ID);
        return list(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeById(Serializable id) {
        resourceService.deleteByTypeId(id);
        return super.removeById(id);
    }
}
